[TOC]

Accessing Structure

To demonstrate techniques to access fields of structure arrays, we use the following example. The structure array cars created below contains 4 cars made by different manufacturers with different engine configurations. Each of the cars has 4 tires: Front-left (FL), front-right (FR), rear-left (RL) and rear-right (RR).

Input
mazda2.manufacturer = 'mazda';
mazda2.engine = struct('diesel', false, 'capacity', 1400, 'turbo', false);
mazda2.tire(1) = struct('brand', 'yokohama', 'position', 'FL');
mazda2.tire(2) = struct('brand', 'yokohama', 'position', 'FR');
mazda2.tire(3) = struct('brand', 'yokohama', 'position', 'RL');
mazda2.tire(4) = struct('brand', 'yokohama', 'position', 'RR');

corolla.manufacturer = 'toyoto';
corolla.engine = struct('diesel', false, 'capacity', 1500, 'turbo', false);
corolla.tire(1) = struct('brand', 'toyo', 'position', 'FL'); 
corolla.tire(2) = struct('brand', 'toyo', 'position', 'FR'); 
corolla.tire(3) = struct('brand', 'toyo', 'position', 'RL'); 
corolla.tire(4) = struct('brand', 'toyo', 'position', 'RR'); 

model3.manufacturer = 'tesla';
model3.engine = 'electric';
model3.tire(1) = struct('brand', 'michelin', 'position', 'FL');
model3.tire(2) = struct('brand', 'michelin', 'position', 'FR');
model3.tire(3) = struct('brand', 'michelin', 'position', 'RL');
model3.tire(4) = struct('brand', 'michelin', 'position', 'RR');

prius.manufacturer = 'toyota';
prius.engine = 'hybrid';
prius.tire(1) = struct('brand', 'goodyear', 'position', 'FL'); 
prius.tire(2) = struct('brand', 'goodyear', 'position', 'FR'); 
prius.tire(3) = struct('brand', 'goodyear', 'position', 'RL'); 
prius.tire(4) = struct('brand', 'goodyear', 'position', 'RR'); 

% 2 x 2 structure array
cars = [mazda2, corolla; model3 prius]

The array cars is a ​ matrix of 4 car structures, namely, mazda2, corolla, model3 and prius.

Getting Field Names

To check out all field names of the array cars, we simply type cars. It shows that each of the car structures in the array has three fields, namely, engine, manufacturer and tire.

Output
cars =
 struct array (2 x 2) with fields:
 engine, tire, manufacturer

The previous output showed all cars. It is possible to check out just a subarray of cars by using, say, cars(1:2). It gives a subarray of cars:

ans =
 struct array (1 x 2) with fields:
 engine, tire, manufacturer

Getting Field Values

So far, we have just seen the field names only. To show their data types and sizes, we need to check out an individual structure (scalar). To access individual element from cars, we use linear or subscript indexing. For examples, we type cars(1) and cars(1,1) to show

ans =
 struct with fields:
       engine: Struct [1 x 1]
         tire: Struct [1 x 4]
 manufacturer: ['mazda']

We see that this is a Mazda. We access fields of an individual structure using the . operator. In the code below, we print out the engine of cars(1), and show whether the engine has turbo or not.

Input
% Engine
cars(1).engine
% Is it a turbo engine?
cars(1).engine.turbo
Output
ans =
 struct with fields:
   diesel: [0]
 capacity: [1400.0]
    turbo: [0]

ans = 
0

Then, we want to show the front tires (1st and 2nd elements of the tire array). Both of the front tires are from the company Yokohama:

Input
cars(1).tire(1)
cars(1).tire(2)
Output
ans =
 struct with fields:
    brand: ['yokohama']
 position: ['FL']

ans =
 struct with fields:
 position: ['FR']
    brand: ['yokohama']

It is possible to access a field using a character array that contains the field name. Instead of typing cars(1).tire(1), we use cars(1).('tire')(1) or cars(1).(t)(1) where t is the character array tire. This is shown in the code segment below:

Input
% Field names stored in character arrays.
t = 'tire';
pos = 'position';
% All of the following are okay.
cars(1).(t)(1).(pos)
cars(1).('tire')(1).('position')
cars(1).tire(1).position
Output
ans = 
FL

ans = 
FL

ans = 
FL

It is not possible to access structure(indexes).field(index), where indexes contains multiple indexes. It is because field may have different sizes (or data types) in different elements of structure. Hence, it may happen that one of structure(1).field and structure(2).field is empty but the other is not empty. In this situation, attempting to access structure(1).field(index) and structure(2).field(index) with the same index may give an ambiguous result. To prevent this problem, accessing structure(1:2).field(index) is not allowed.

This is demonstrated in the example below. Here a is a structure vector of 2 elements. A random matrix is assigned to a(2).b. Note that it is only assigned to a(2), therefore a(1).b is an empty array. As a result, a(1).b and a(2).b have different sizes, namely, and , respectively. Accessing a(1:2).b(1) is not allowed. For the same reason, a.b(1) is also not allowed.

clear
a(2).b = rand(3)
% a(1).b and a(2).b have different sizes.
a(1).b
a(2).b
% a(1).b(1) and a(2).b(1) should not be accessed together
% Hence, the following will fail.
a(1:2).b(1)
All variables cleared.

a =
 struct array (1 x 2) with field:
 b

ans =
 [] (double array)

ans = 1e-1 × 
 3.2403   3.4023   5.6312
 6.0383   6.4616   0.7474
 7.2696   0.8609   4.8245

All variables cleared.

a =
 struct array (1 x 2) with field:
 b

ans =
 [] (double array)

ans = 1e-1 × 
 3.2403   3.4023   5.6312
 6.0383   6.4616   0.7474
 7.2696   0.8609   4.8245

Error at Line 8(1). Calling .b of multiple structures returns multiple fields. Indexing multiple fields at the same time is not allowed.

Getting Field Values of All Structures

In our first cars examples, there are 4 cars in the structure array cars. Executing cars.manufacturer will give the manufacturer of the first car only:

Input
cars.manufacturer
Output
ans = 
mazda

To get the other manufacturers, we need to assign each of them to a separate variable:

Input
[a,b,c]=cars.manufacturer
Output
a = 
mazda

b = 
tesla

c = 
toyoto

In the following example, we get all of the engine structures:

Input
[s1, s2, s3] = cars.engine
Output
s1 =
 struct with fields:
 capacity: [1400.0]
    turbo: [0]
   diesel: [0]

s2 = 
electric

s3 =
 struct with fields:
   diesel: [0]
    turbo: [0]
 capacity: [1500.0]